import 'package:flutter/material.dart';

class SearchBarWidget extends StatefulWidget {
  final Function? locationFunc;

  final bool? showMap;

  final Function? backFunction;

  final Function(String searchWorld)? searchFunc;

  SearchBarWidget({this.locationFunc, this.showMap, this.backFunction, this.searchFunc});

  @override
  State createState() {
    return _SearchBarWidget();
  }
}

class _SearchBarWidget extends State<SearchBarWidget> {

  String _searchWorld = '';

  late TextEditingController _controller;


  @override
  void initState() {
    setState(() {
      _controller = TextEditingController();
      _searchWorld = '';
    });
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Expanded(
          child: Container(
            height: 36,
            padding: const EdgeInsets.only(top: 4),
            decoration: BoxDecoration(
              color: Colors.grey[200],
              borderRadius: BorderRadius.circular(18)
            ),
            child: TextField(
              onSubmitted: (value){
                setState(() {
                  _searchWorld = value;
                  widget.searchFunc!(value);
                });
              },
              controller: _controller,
              textInputAction: TextInputAction.search,
              decoration: InputDecoration(
                  border: InputBorder.none,
                  hintText: '请输入搜索关键字',
                  hintStyle: const TextStyle(fontSize: 14),
                  prefixIcon: const Icon(Icons.search,size: 20,color: Colors.grey,),
                  suffixIcon: GestureDetector(
                    child: const Icon(Icons.clear,size: 15,color: Colors.grey,),
                    onTap: () {
                      setState(() {
                        _searchWorld = '';
                        _controller.clear();
                      });
                    },
                  ),
              ),
            ),
          ),
        ),
        SizedBox(width: 5,),
        widget.showMap==true?const Icon(Icons.explore,color: Colors.green):Container(),
      ],
    );
  }
}
